package com.example.beetobe.schedule;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.example.beetobe.R;

public class TimeScheduleActivity extends Activity implements OnTouchListener {

	GridLayout glSchedule;
	ImageView[][] scheduleCell;
	int prevIndCol = -10;
	int prevIndRow = -10;
	ImageButton iBtnEdit;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_schedule);
        
        scheduleCell = new ImageView[34][7];
		glSchedule = (GridLayout) findViewById(R.id.Grid_Schdule);
		glSchedule.setOnTouchListener(this);
		
		iBtnEdit = (ImageButton) findViewById(R.id.ImgButton_TouchTab);
		iBtnEdit.setBackgroundDrawable(null); 
		
		
		
		for (int i = 0; i < 34; i++) {
			for (int j = 0; j < 7; j++) {
				scheduleCell[i][j] = new ImageView(this);
				scheduleCell[i][j]
						.setBackgroundResource(R.drawable.non_selected);
				scheduleCell[i][j].setContentDescription("1");
				glSchedule.addView(scheduleCell[i][j]);
			}
		}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_time_schedule, menu);
        return true;
    }
    
    public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		//
		if (v.getId() == R.id.Grid_Schdule) {
			if (event.getAction() == MotionEvent.ACTION_DOWN
					|| event.getAction() == MotionEvent.ACTION_MOVE) {
				float pntX = event.getX();
				float pntY = event.getY();

				int indCol = (int) (pntX / scheduleCell[0][0].getWidth());
				int indRow = (int) (pntY / scheduleCell[0][0].getHeight());

			
				if (indRow >= 0 && indCol >= 0 && indRow < 34 && indCol < 7) {
					if (prevIndCol != indCol || prevIndRow != indRow) {
						String content = scheduleCell[indRow][indCol]
								.getContentDescription().toString();
						if (content.equals("1")) {
							scheduleCell[indRow][indCol]
									.setBackgroundResource(R.drawable.selected);
							scheduleCell[indRow][indCol]
									.setContentDescription("0");
						} else if (content.equals("0")) {
							scheduleCell[indRow][indCol]
									.setBackgroundResource(R.drawable.non_selected);
							scheduleCell[indRow][indCol]
									.setContentDescription("1");
						}
						prevIndCol = indCol;
						prevIndRow = indRow;
						Log.d("Point", "Row : " + indRow + ", Col : " + indCol);
					}
				}

			}
		}

		return true;
	}
}
